﻿using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;

using ProjectBase.Tools.Publishing.Handlers.ExceptionHandler;

namespace $safeprojectname$.Infrastructure.Factories
{
    public static class ExceptionConfigurationReader
    {
        public static XElement DefaultExceptionHandlerSettings()
        {
            return GetSectionExample("settings", "settings");
        }

        public static XElement NotCompleteExceptionHandlerSettings()
        {
            return GetSectionExample("settingsNotComplete", "settings");
        }
        public static XElement MissingExceptionHandlerSettings()
        {
            return GetSectionExample("settingsMissing", "settings");
        }

        #region Read Configuration For

        public static ExceptionConfiguration ForDefault()
        {
            var element = GetSectionExample("default");
            return new ConfigurationReader().Create(element);
        }
        public static ExceptionConfiguration ForBrokenEventId()
        {
            var element = GetSectionExample("brokenEventId");
            return new ConfigurationReader().Create(element);
        }
        public static ExceptionConfiguration ForIncorrectElement()
        {
            return new ConfigurationReader().Create("Log4Net.config");
        }
        public static ExceptionConfiguration ForMissingType()
        {
            var element = GetSectionExample("missingType");
            return new ConfigurationReader().Create(element);
        }
        public static ExceptionConfiguration ForExceptionMissing()
        {
            var element = GetSectionExample("exceptionsMissing");
            return new ConfigurationReader().Create(element);
        }
        #endregion Read Configuration For

        public static XElement GetSectionExample(string sectionName, string elementName = null)
        {
            elementName = elementName ?? "exceptionManagement";
            var path = Path.Combine(Environment.CurrentDirectory, "TestingExamples.config");
            if (!File.Exists(path))
            {
                path = Path.Combine(Environment.CurrentDirectory, "Config/TestingExamples.config");
            }
            var xdoc = XDocument.Load(path);
            return xdoc.Root
                .Elements(elementName)
                .Where(e => e.Attribute("name").Value.Equals(sectionName))
                .First();
        }
    }
}
